package com.wnzt.baszh.util.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 加密和解密方法二
 * @author dqw
 * @date 2017-3-22
 */
public class encode {
     private static final int numOfEncAndDec = 0x99; //加密解密秘钥
        private static int dataOfFile = 0; //文件字节内容
        
        public static void main(String[] args) {
            File srcFile = new File("D:/Download/1.jpg"); //初始文件
            File encFile = new File("D:/Download/text4444444.jpg"); //加密文件
            File decFile = new File("D:/Download/4.jpg"); //解密文件
            
            try {
               EncFile(srcFile, encFile); //加密操作
               // DecFile(encFile, decFile); //加密操作
            } catch (Exception e) {
                e.printStackTrace();
            }  
        }
        public static void EncFile(File srcFile, File encFile) throws Exception {
            if(!srcFile.exists()){
                System.out.println("source file not exixt");
                return;
            }
            
            if(!encFile.exists()){
                System.out.println("encrypt file created");
                encFile.createNewFile();
            }
            InputStream fis  = new FileInputStream(srcFile);
            OutputStream fos = new FileOutputStream(encFile);
            
            while ((dataOfFile = fis.read()) > -1) {
                fos.write(dataOfFile^numOfEncAndDec);
            }
            srcFile.delete();
            fis.close();
            fos.flush();
            fos.close();
        }
        
    public static void DecFile(File encFile, File decFile) throws Exception {
        if (!encFile.exists()) {
            System.out.println("encrypt file not exixt");
            return;
        }
        if (!decFile.exists()) {
            System.out.println("decrypt file created");
            decFile.createNewFile();
        }
        InputStream fis = new FileInputStream(encFile);
        OutputStream fos = new FileOutputStream(decFile);
        while ((dataOfFile = fis.read()) > -1) {
            fos.write(dataOfFile ^ numOfEncAndDec);
        }
        fis.close();
        fos.flush();
        fos.close();
    }
}